package com.daidai.connectors;

import com.daidai.sink.domain.User;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.Properties;

public class KafkaToKafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "daidai:9092");
        properties.setProperty("group.id", "flink-test");

        //kafkaproducer
        FlinkKafkaProducer flinkKafkaProducer = new FlinkKafkaProducer("flink-test", new SimpleStringSchema(), properties);
        DataStreamSource<String> dataStreamSource = env.fromElements(new User(1, "daidai", "admin").toString());
        dataStreamSource.addSink(flinkKafkaProducer);


        //kafkaconsumer
        FlinkKafkaConsumer<String> flinkKafkaConsumer = new FlinkKafkaConsumer<>("flink-test", new SimpleStringSchema(), properties);
        flinkKafkaConsumer.setStartFromLatest();
        DataStreamSource<String> source = env.addSource(flinkKafkaConsumer);

        source.print();

        env.execute();
    }
}
